<?php

namespace app\api\controller;


use Ayesh\InstagramDownload\InstagramDownload;
use CURLFile;
use GuzzleHttp\Client;
use InstagramScraper\Exception\InstagramAuthException;
use InstagramScraper\Exception\InstagramChallengeRecaptchaException;
use InstagramScraper\Exception\InstagramChallengeSubmitPhoneNumberException;
use InstagramScraper\Exception\InstagramException;
use InstagramScraper\Exception\InstagramNotFoundException;
use InstagramScraper\Instagram;
use Psr\SimpleCache\InvalidArgumentException;
use think\facade\Env;
use Phpfastcache\Helper\Psr16Adapter;

class Index extends Base
{
    public function index()
    {
        echo 'api';
    }

    public function getmediafromins()
    {
        //\curl_setopt($curl,\CURLOPT_COOKIE,"Domain=.instagram.com; rur=NAO; ds_user_id=5922691420; sessionid=5922691420%3ATmp6umJQ7vKzzI%3A25; csrftoken=wtxIScmbU4NXGKWKk8znUo2u1moFFNDT");
        $url = isset($_POST['url']) ? $_POST['url'] : '';
        if (!empty($url)) {
            try {
                $client = new InstagramDownload($url);
                $url    = $client->getDownloadUrl();
                $type   = $client->getType();

                $data['type'] = $type;
                $url          = downloadLocal($url);
                if (!$url['code']) {
                    $data['code'] = 1000;
                    $data['msg']  = 'error!<br>' . $url['msg'];
                    echo json_encode($data);
                    exit();
                }
                $data['url']  = $url;
                $data['type'] = $type;
                $data['code'] = 1001;
                $data['msg']  = 'success';

                echo json_encode($data);
            } catch (\InvalidArgumentException $e) {
                $data['code'] = 1000;
                $data['msg']  = 'error：InvalidArgumentException，' . $e->getMessage();

                echo json_encode($data);
            } catch (\RuntimeException $e) {
                $data['code'] = 1000;
                $data['msg']  = 'error：RuntimeException，' . $e->getMessage();

                echo json_encode($data);
            }
        } else {
            $data['code'] = 1000;
            $data['msg']  = 'error:无链接';

            echo json_encode($data);
        }
    }

    public function GetmediafrominsTest()
    {
        $api = file_get_contents("https://api.instagram.com/oembed/?url=https://www.instagram.com/p/CNY52r5Hqcw/");
        dump($api);
        exit();
        $url = isset($_POST['url']) ? $_POST['url'] : '';
        if (empty($url)) {
            $data['code'] = 1000;
            $data['msg']  = 'error:无链接';

            echo json_encode($data);
            exit();
        }
        //$instagram = new Instagram(new Client());
        $instagram = Instagram::withCredentials(new Client(), '569349641@qq.com', 'a2288215', new Psr16Adapter('Files'));
        $instagram->setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36');
        try {
            $instagram->login(false, true);
            $instagram->saveSession();
        } catch (InstagramAuthException $e) {
            $data['code'] = 1000;
            $data['msg']  = 'error：InstagramAuthException，' . $e->getMessage();
            echo json_encode($data);
            exit();
        } catch (InstagramChallengeRecaptchaException $e) {
            $data['code'] = 1000;
            $data['msg']  = 'error：InstagramChallengeRecaptchaException，' . $e->getMessage();

            echo json_encode($data);
            exit();
        } catch (InstagramChallengeSubmitPhoneNumberException $e) {
            $data['code'] = 1000;
            $data['msg']  = 'error：InstagramChallengeSubmitPhoneNumberException，' . $e->getMessage();

            echo json_encode($data);
            exit();
        } catch (InstagramException $e) {
            $data['code'] = 1000;
            $data['msg']  = 'error：InstagramException，' . $e->getMessage();

            echo json_encode($data);
            exit();
        } catch (InvalidArgumentException $e) {
            $data['code'] = 1000;
            $data['msg']  = 'error：InvalidArgumentException，' . $e->getMessage();

            echo json_encode($data);
            exit();
        }

        try {
            $media = $instagram->getMediaByUrl($url);
            echo json_encode($media);
        } catch (InstagramException $e) {
            $data['code'] = 1000;
            $data['msg']  = 'error：InstagramException，' . $e->getMessage();

            echo json_encode($data);
        } catch (InstagramNotFoundException $e) {
            $data['code'] = 1000;
            $data['msg']  = 'error：InstagramNotFoundException，' . $e->getMessage();

            echo json_encode($data);
        }
    }

    public function test()
    {
        return json_encode(uploadToSpace("https://scontent.cdninstagram.com/v/t51.2885-15/e35/p1080x1080/170022655_274894797629981_633790373104178543_n.jpg?tp=1&_nc_ht=scontent.cdninstagram.com&_nc_cat=105&_nc_ohc=xZRWFPWCgqoAX-lv4SB&edm=AABBvjUAAAAA&ccb=7-4&oh=a5da0006d39035f5ebcca5a861d08f76&oe=609A90AC&_nc_sid=83d603"));
    }

    public function ssjss($url = 'http://imgtools.epizy.com/index/api')
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $response = curl_exec($ch);// 已经获取到内容，没有输出到页面上。
        dump($response);
        curl_close($ch);
        echo $response;
    }
}